package com.y3k.dao;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import org.apache.log4j.Logger;

import com.y3k.utils.FileUtil;

public class FileDao {

	public static final Logger log = Logger.getLogger(FileDao.class);

	public String save(String url, byte[] datos) throws Exception {
		url = url.substring(0, url.indexOf("Dashboard.html"));
		URL resorces_url = Thread.currentThread().getContextClassLoader().getResource("/");
		String exportedFilePath = resorces_url.getPath().substring(0, resorces_url.getPath().indexOf("WEB-INF"));

		FileUtil.deleteFromDirectory(exportedFilePath, ".xls");

		String fileName = "excel" + System.currentTimeMillis() + ".xls";
		FileOutputStream out = new FileOutputStream(new File(exportedFilePath + fileName));
		out.write(datos);
		out.close();

		return url + fileName;
	}

}